package cn.tedu.tnt_sky.service.Impl;

import cn.tedu.tnt_sky.common.context.BaseContext;
import cn.tedu.tnt_sky.common.response.MessageConstant;
import cn.tedu.tnt_sky.exception.AccountExistsException;
import cn.tedu.tnt_sky.exception.AccountNotFoundException;
import cn.tedu.tnt_sky.exception.PasswordErrorException;
import cn.tedu.tnt_sky.mapper.admin.AdminMapper;
import cn.tedu.tnt_sky.pojo.dto.*;
import cn.tedu.tnt_sky.pojo.entity.Admin;
import cn.tedu.tnt_sky.pojo.vo.AdminSettingsVO;
import cn.tedu.tnt_sky.pojo.vo.AdminVO;
import cn.tedu.tnt_sky.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;

@Slf4j
@Service  // TNTBlog2077
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;

    /**
     * 管理员登录
     *
     * @param adminLoginDTO
     * @return
     */
    @Override
    public Admin AdminLogin(AdminLoginDTO adminLoginDTO) {
        Admin admin = adminMapper.selectByUsername(adminLoginDTO.getUsername());

        if (admin == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        // md5 加密
        String password = DigestUtils.md5DigestAsHex(adminLoginDTO.getPassword().getBytes(StandardCharsets.UTF_8));
        if (!password.equals(admin.getPassword())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        return admin;
    }

    /**
     * 管理员个人信息查询
     *
     * @param username
     * @return
     */
    @Override
    public AdminSettingsVO settingSelect(String username) {
        Admin admin = adminMapper.selectByUsername(username);
        AdminSettingsVO adminSettingsVO = new AdminSettingsVO();
        BeanUtils.copyProperties(admin, adminSettingsVO);
        return adminSettingsVO;
    }

    /**
     * 管理员个人信息修改
     *
     * @param adminUpdateNicknameDTO
     */
    @Override
    public void updateSettingNickname(AdminUpdateNicknameDTO adminUpdateNicknameDTO) {
        Admin admin = adminMapper.selectByUsername(adminUpdateNicknameDTO.getUsername());
        if (admin == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        if (adminUpdateNicknameDTO.getNickname() == null) {
            throw new AccountExistsException(MessageConstant.NiCK_NAME_NULL);
        }
        AdminPutDTO adminPutDTO = new AdminPutDTO();
        BeanUtils.copyProperties(adminUpdateNicknameDTO,adminPutDTO);

        adminMapper.updateSetting(adminPutDTO);
    }

    @Override
    public void updateSettingPassword(AdminUpdatePasswordDTO adminUpdatePasswordDTO) {
        Admin admin = adminMapper.selectByUsername(adminUpdatePasswordDTO.getUsername());
        if (admin == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        String passwordOut = DigestUtils.md5DigestAsHex(adminUpdatePasswordDTO.getPasswordOut().getBytes(StandardCharsets.UTF_8));

        if (!passwordOut.equals(admin.getPassword())){
            throw  new PasswordErrorException(MessageConstant.PASSWORD_ERROR_OUT);
        }
        String password = DigestUtils.md5DigestAsHex(adminUpdatePasswordDTO.getPassword().getBytes(StandardCharsets.UTF_8));
        adminUpdatePasswordDTO.setPassword(password);
        AdminPutDTO adminPutDTO = new AdminPutDTO();
        BeanUtils.copyProperties(adminUpdatePasswordDTO,adminPutDTO);
        adminMapper.updateSetting(adminPutDTO);
    }

    /**
     * 头像上传
     */
    @Override
    public void uploadById(AdminUploadDTO adminUploadDTO) {
        long id = BaseContext.getCurrentId();
        adminUploadDTO.setId(id);
        adminMapper.uploadById(adminUploadDTO);
    }

    /**
     * 刷新数据
     * @return
     */
    @Override
    public AdminVO refresh() {
        long id = BaseContext.getCurrentId();
        return adminMapper.refresh(id);
    }
}
